package com.applet.mallappletbackstage.controller;


import com.applet.mallappletbackstage.entity.Collect;
import com.applet.mallappletbackstage.entity.ReleaseWork;
import com.applet.mallappletbackstage.service.CollectService;
import com.applet.mallappletbackstage.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * (Collect)表控制层
 *
 * @author makejava
 * @since 2020-08-17 10:06:07
 */
@RestController
@RequestMapping("/collect")
public class CollectController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private CollectService collectService;

    /**
     * 分页查询所有数据
     *
     * @param current    分页对象
     * @param
     * @return 所有数据
     */
    @GetMapping("page/{current}/{user}")
    public R selectAll(@PathVariable Long current, @PathVariable String user, @RequestParam String typeOfWork) {
        IPage<ReleaseWork> page = collectService.selectAll(current, user, typeOfWork);
        if (page == null) {
            return R.fail("没有数据");
        }
        return R.success().data(page);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @GetMapping("{user}/{work}")
    public R selectOne(@PathVariable String user, @PathVariable String work) {
        QueryWrapper<Collect> collectQueryWrapper = new QueryWrapper<>();
        collectQueryWrapper.eq("user", user);
        collectQueryWrapper.and(collectQueryWrapper1 -> collectQueryWrapper1.eq("work", work));
        Collect collectServiceOne = this.collectService.getOne(collectQueryWrapper);
        return R.success(collectServiceOne);
    }

    /**
     * 新增数据
     *
     * @param collect 实体对象
     * @return 新增结果
     */
    @PostMapping("save")
    public R insert(@RequestBody Collect collect) {
        return R.success(this.collectService.save(collect));
    }

    /**
     * 修改数据
     *
     * @param collect 实体对象
     * @return 修改结果
     */
    @PutMapping
    public R update(@RequestBody Collect collect) {
        return R.success(this.collectService.updateById(collect));
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @DeleteMapping
    public R delete(@RequestParam String id) {
        return R.success(this.collectService.removeById(id));
    }
}
